package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.controller.PageResult;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult page(Integer page, Integer pageSize, String name, Integer degree, Integer clazzId) {
        // 开启分页
        PageHelper.startPage(page, pageSize);
        // 调用 mapper 分页查询
        Page<Student> studentsPage = studentMapper.page(name, degree, clazzId);
        // 封装 PageResult 并返回
        return new PageResult(studentsPage.getTotal(), studentsPage.getResult());
    }

    @Override
    public void deleteByIds(List<Integer> idList) {
        studentMapper.deleteByIds(idList);
    }

    @Override
    public void save(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void update(Student student) {
        studentMapper.updateById(student);
    }

    @Override
    public void addViolation(Integer id, Integer score) {
       studentMapper.addViolation(id,score);
    }
}
